package cn.yqn.basic.controller;

import cn.yqn.basic.service.IVerifyCodeService;
import cn.yqn.basic.exception.BusinessException;
import cn.yqn.basic.utils.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
* @Description: 登录信息验证
* @Author: Mr.She
* @Version: 1.0
* @Date:  2022/7/28 15:19
*/
@RequestMapping("/verifyCode")
@RestController
public class VerifyCodeController {

    @Autowired
    private IVerifyCodeService verifyCodeService;

    /** 
    * @Title: 绑定验证
    * @Description: TODO
    * @Author: YQN
    * @Version: 1.0
    * @Date:  2022/8/1 16:44  
    * @Parameters: [map]
    * @Return cn.yqn.basic.utils.JsonResult
    */
    @PostMapping("/binderSmsCode")
    public JsonResult binderSmsCode(@RequestBody Map<String,String> map){
        try {
            verifyCodeService.binderSmsCode(map);
            return JsonResult.my();
        }catch (BusinessException e) {
            e.printStackTrace();
            return JsonResult.my().setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.my().setMessage("系统异常，稍后重试");
        }
    }


    /**
     * @Description: 图片验证码
     * @Author: Mr.She
     * @Version: 1.0
     * @Date:  2022/7/28 0:09
     */
    @GetMapping("/image/{key}")
    public JsonResult getVerifyCode(@PathVariable("key") String key) {
        try {
            String baseStr = verifyCodeService.getVerifyCode(key);
            return JsonResult.my().setResultObj(baseStr);
        } catch (BusinessException e) {
            e.printStackTrace();
            return JsonResult.my().setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.my().setSuccess(false).setMessage("图片生成失败,请稍后重试");
        }
    }

    /**
    * @Title: 短信验证码
    * @Description: TODO
    * @Author: YQN
    * @Version: 1.0
    * @Date:  2022/7/28 15:19
    * @Parameters: [map]
    * @Return cn.yqn.basic.utils.JsonResult
    */
    @PostMapping("/smsCode")
    public JsonResult smsCode(@RequestBody Map<String, String> map) {
        try {
            verifyCodeService.smsCode(map);
            return JsonResult.my();
        } catch (BusinessException e) {
            e.printStackTrace();
            return JsonResult.my().setMessage(e.getMessage());
        } catch (Exception e) {
            return JsonResult.my().setMessage("操作失败,请稍后重试!");
        }
    }

}
